from entity.user import User
from utils.dbpools_utils import db_pool


def insert_user_info(user: User) -> int:
    sql = "INSERT INTO users(name, password) \
       VALUES ('%s', '%s')" % (user.username, user.password)
    db_pool.update_data(sql)


def search_user_info(user: User):
    sql = " SELECT * FROM users\
     WHERE name='%s' and password = '%s'" % (user.username, user.password)
    return db_pool.fetch_data(sql)


def search_user_info_by_name(name: str):
    sql = " SELECT * FROM users\
     WHERE name='%s'" % name
    return db_pool.fetch_data(sql)


def select_all_common():
    sql = "SELECT * FROM users WHERE name <> 'admin' AND name <> 'walker'"
    return db_pool.fetch_data(sql)


def delete_user_info(username: str):
    sql = "DELETE From users where name = '%s'" % username
    db_pool.update_data(sql)


def update_user_info(username: str):
    sql = "UPDATE users SET password='000' WHERE name='%s'" % username
    db_pool.update_data(sql)


def get_total_user_count():
    sql = "SELECT COUNT(*) FROM users "
    res = db_pool.fetch_data(sql)
    return res[0][0]
